from django.db import models
from ckeditor.fields import RichTextField
from django.urls import reverse

from user.models import CustomUser


# Create your models here.
class Category(models.Model):
    title = models.CharField(max_length=120,verbose_name='分类名')
    parent = models.ForeignKey('self',verbose_name='父类', on_delete=models.CASCADE, null=True, blank=True)
    def __str__(self):
        return self.title
    def children(self):
        return Category.objects.filter(parent=self)

class Tag(models.Model):
    title = models.CharField(verbose_name="标签名",max_length=120,unique=True)
    def __str__(self):
        return self.title


class Article(models.Model):
    title = models.CharField(verbose_name='标题',max_length=100)
    content = RichTextField(verbose_name='正文')
    # content = models.TextField(verbose_name='正文')
    create_time = models.DateTimeField(verbose_name='create_time',auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='update_time',auto_now=True)
    categories = models.ManyToManyField(verbose_name="所属分类",to='Category')
    tags = models.ManyToManyField(verbose_name="所属标签",to='Tag')
    author = models.ForeignKey(verbose_name="作者",to=CustomUser,on_delete=models.CASCADE)
    state = models.CharField(verbose_name="审核状态",max_length=10,choices=(
    ('success',"已通过"),
    ('fail',"已驳回"),
    ('undo',"未处理")
    ),default='undo')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('main:article_list')


    class Meta:
        ordering = ['-create_time']